package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.mapper.MenuMapper;
import com.li.mapper.RoleMapper;
import com.li.pojo.Menu;
import com.li.pojo.Role;
import com.li.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    public void add(Menu menu) {
        menuMapper.add(menu);
    }

    public void deleteById(Integer id) {
        menuMapper.deleteById(id);
    }

    public void updateById(Menu menu) {
        menuMapper.updateById(menu);
    }

    public Menu findById(Integer id) {
        Menu menu = menuMapper.findById(id);
        System.out.println(menu);
        return menu;
    }

    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        // 设置分页
        PageHelper.startPage(currentPage, pageSize);
        // 查询数据库
        Page<Menu> pages = menuMapper.findAllByQueryString(queryPageBean.getQueryString());
        long total = pages.getTotal();
        List<Menu> menuList = pages.getResult();
        return new PageResult(total, menuList);
    }

    public List<Menu> findMenusByUserId(Integer userId) {

        // 现根据id查询该用户对应的主菜单
        List<Menu> menus = menuMapper.findMenusByUserId(userId);
        for (Menu menu : menus) {
            Integer parentMenuId = menu.getId();
            if (parentMenuId != null) {
                // 查询子菜单
                List<Menu> children = menuMapper.findMenusByParentMenuId(parentMenuId);
                menu.setChildren(children);
            }
        }
        return menus;
    }


    public List<Menu> getMenuList(int id)  {

        Set<Role> roles = roleMapper.findByUserId(id);
        List<Menu> menus = menuMapper.findByRoles(roles);

        for (int i = 0; i < menus.size(); i++) {
            Menu menu = menus.get(i);
            findSetChildren(menus, menu);
        }

        List<Menu> parentMenuList = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentMenuId() == null){
                parentMenuList.add(menu);
            }
        }

        return parentMenuList;

    }

    private void findSetChildren(List<Menu> menus, Menu parentMenu) {
        for (Menu menu : menus) {
            if (parentMenu.getId().equals(menu.getParentMenuId())) {
                List<Menu> children = parentMenu.getChildren();
                children.add(menu);

            }
        }

    }

    @Override
    public  List<Map<String, Object>> findParentMenuIds() {
        List<Integer> parentMenuIds = menuMapper.findParentMenuIds();
        List<Map<String, Object>> maps = new ArrayList<>();
        for (int i = 0; i < parentMenuIds.size(); i++) {
            Map map = new HashMap();
            map.put("value" , "选项"+(i+1) );
            map.put("label" , parentMenuIds.get(i));
            maps.add(map);
        }
        return maps;
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuMapper.findAllMenu();
    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer roleId) {
        return menuMapper.findMenuIdsByRoleId(roleId);
    }
}
